package com.mentaleye.controller;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.ReturnVisitBean;
import com.mentaleye.biz.*;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by Rock on 2017/5/18.
 */
@Controller
@RequestMapping("/returnvisit")
public class ReturnVisitController {

    @Autowired private CompanyStudentBiz companyStudentBiz;
    @Autowired private ReturnVisitBiz returnVisitBiz;
    @Autowired private CompanyBiz companyBiz;
    @Autowired private DictionaryBiz dictionaryBiz;

    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView view = new ModelAndView("em/returnvisit/returnVisitList");
        Map<Integer,String>   recruitMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.IS_RECRUIT.getCode());
        Map<Integer, String> studyMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.IS_STUDY.getCode());
        Map<Integer, String> workStateMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.WORK_STATE.getCode());
        view.addObject("recruitMap", recruitMap);
        view.addObject("studyMap", studyMap);
        view.addObject("workStateMap", workStateMap);
        view.addObject("workState" ,JSONObject.toJSONString(workStateMap));
        view.addObject("recruit" ,JSONObject.toJSONString(recruitMap));
        view.addObject("study" ,JSONObject.toJSONString(studyMap));
        return view;
    }

    @RequestMapping("/listData")
    public @ResponseBody String listData(CommonPage commonPage){
        CommonBizImpl.queryByPage("ReturnVisit",commonPage);
        return commonPage.toSHowPage();
    }

    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam(value="returnVisitId", required=false) Integer returnVisitId) {
        ModelAndView view = new ModelAndView("em/returnvisit/returnVisitEdit");
        Map<Integer,String>   recruitMap=dictionaryBiz.queryDictionaryByParent(DictionaryEnums.IS_RECRUIT.getCode());
        Map<Integer, String> studyMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.IS_STUDY.getCode());
        Map<Integer, String> workStateMap = dictionaryBiz.queryDictionaryByParent(DictionaryEnums.WORK_STATE.getCode());
        view.addObject("recruitMap", recruitMap);
        view.addObject("studyMap", studyMap);
        view.addObject("workStateMap", workStateMap);
        view.addObject("companyList",companyBiz.getBeanList());
        view.addObject("studentList",companyStudentBiz.queryByState(
                DictionaryEnums.EMPLOYMENT.getCode(),DictionaryEnums.NOT_WORK.getCode()));
        if (returnVisitId != null) {
            ReturnVisitBean bean = (ReturnVisitBean) ObjectUtil.transfer(CommonBizImpl.getBeanById(
                            "ReturnVisit", "returnVisitId", returnVisitId),ReturnVisitBean.class);
            Map<String, Object> company = companyStudentBiz.queryDataByReturnVisitId(returnVisitId);
            if (company != null) {
                bean.setCompanyId(Integer.valueOf(company.get("companyId").toString()));
                bean.setCompanyName(company.get("companyName").toString());
                bean.setStudentName(company.get("studentName").toString());
                bean.setWorkState(Integer.valueOf(company.get("workState").toString()));
            }
            view.addObject("bean", bean);
            view.addObject("edit",true);
        }
        return view;
    }

    @RequestMapping(value = "/save")
    public @ResponseBody String save( ReturnVisitBean bean) {
        CommonResult result;
        if (bean.getReturnVisitId() != null){
            result = CommonBizImpl.edit(bean, "ReturnVisit", "returnVisitId", null);
            if (result.getIsSuccess()){
                return JSONObject.toJSONString(returnVisitBiz.editRelation(bean,false));
            }
        }else{
            result = returnVisitBiz.add(bean);
            if (result.getIsSuccess()){
                return JSONObject.toJSONString(returnVisitBiz.editRelation(bean,true));
            }
        }
        return JSONObject.toJSONString(result);
    }

    @RequestMapping("/delete")
    public @ResponseBody  String delete(@RequestBody List<Integer> ids){
        CommonResult result = CommonBizImpl.delete(
                "ReturnVisit",ids,"returnVisitId",null);
        if (result.getIsSuccess()){
            return JSONObject.toJSONString(companyStudentBiz.removeList(ids));
        }
        return JSONObject.toJSONString(result);
    }
}
